package com.example.apple.ddhr_new.utils;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;

import android.view.View;

import com.example.apple.ddhr_new.App;
import com.example.apple.ddhr_new.R;


import androidx.recyclerview.widget.RecyclerView;

/**
 * 列表item之间水平的分割线（分割距离）
 * <p>
 * Created by hudaqiang on 2018/3/2.
 */

public class LineDecoration extends RecyclerView.ItemDecoration {

    private float height = 10.0f; //默认
    private int drawColor = -1;   //默认
    private Paint paint = null;
    private float left = 0.0f; //默认

    public LineDecoration(float height) {
        this(height,  0); //默认
    }
    public LineDecoration(float height,float left) {
        this(height,  App.self().getResources().getColor(R.color.color_line),left); //默认
    }
    public LineDecoration(float height, int drawColor,float left) {
        this.height = height;
        this.left = left;
        this.drawColor = drawColor;
        if (this.drawColor != -1) {
            paint = new Paint();
            paint.setColor(drawColor);
        }
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State
            state) {
        outRect.set( 0, 0, 0, ScreenUtil.dp2px(App.self(), height));
        //outRect.bottom = DensityUtil.dip2px(App.self(), height);
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        super.onDraw(c, parent, state);
        if (drawColor == -1 || paint == null) return;

        int childCount = parent.getChildCount();
        int left = parent.getPaddingLeft()+ScreenUtil.dp2px(App.self(),this.left);
        int right = parent.getWidth() - parent.getPaddingRight();
        for (int i = 0; i < childCount - 1; i++) {
            View view = parent.getChildAt(i);
            float top = view.getBottom();
            float bottom = view.getBottom() + height;
            c.drawRect(left, top, right, bottom, paint);
        }
    }
}
